//
//  UserUtil.swift
//  YoruNeko
//
//  Created by Kotori on 2021/6/23.
//

import Foundation

let defaults = UserDefaults.init(suiteName: "user")

class UserManager {
    private static let anonymousUuid: String = "c6181942-89c3-427e-a814-374dbb0ef45a"
    
    static var currUsername: String {
        currUserMgr.getUsername()
    }
    
    static var currUserUuid: UUID {
        if let curr = defaults?.string(forKey: "currUser") {
            return UUID.init(uuidString: curr) ?? UUID.init(uuidString: anonymousUuid)!
        }
        return UUID.init(uuidString: anonymousUuid)!
    }
    
    static var currUserMgr: UserPreferenceManager {
        get {
            if let currUser = defaults?.string(forKey: "currUser") {
                return UserPreferenceManager.manager(of: UUID.init(uuidString: currUser))
            } else {
                return UserPreferenceManager.manager(of: nil)
            }
        }
    }
    
    //将当前用户UUID写入数据库
    static func setCurrUser(uuidString: String) {
        defaults?.set(uuidString, forKey: "currUser")
    }
}
